import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:location/location.dart';

import 'my_log.dart';

class DeviceInfoChannel {
  static const String tag = "DeviceInfoChannel";
  static final DeviceInfoChannel _instance = DeviceInfoChannel._();

  DeviceInfoChannel._();

  factory DeviceInfoChannel() => _instance;

  static const MethodChannel channel = MethodChannel("deviceInfo.channel");

  static Future<double> getFreeSpace() async {
    return (await channel.invokeMethod("getFreeSpace")).toDouble();
  }

  static Future<double> getTotalSpace() async {
    return (await channel.invokeMethod("getTotalSpace")).toDouble();
  }

  ///定位是否可用
  static Future<bool> isLocEnable() async {
    return await Location().serviceEnabled();
    // if (Platform.isIOS) return false;
    // return await channel.invokeMethod("isLocEnable");
  }

  static Future<bool> openLocService() async {
    try {
      return await Location().requestService();
    } catch (e) {
      return false;
    }
  }

  ///前往定位设置页
  static Future locationSetting() async {
    return await channel.invokeMethod('locationSetting');
  }

  static Future openBluetooth() async {
    return await FlutterBluePlus.turnOn();
  }

  static Future<bool> isBluetoothEnable() async {
    MyLog.i(tag, "isBluetoothEnable");
    try {
      var isEnable = await channel.invokeMethod("isBluetoothEnable");
      MyLog.i(tag, "isBluetoothEnable:$isEnable");
      return isEnable;
    } catch (e) {
      MyLog.e(tag, e.toString());
      return false;
    }
    // return await channel.invokeMethod("isBluetoothEnable");
  }

  static Future startScan() async {
    return await channel.invokeListMethod("startScan");
  }




  static Future<String> getCpuLoad() async {
    double cpuRate = await channel.invokeMethod("getCpuLoad");
    if (cpuRate > 0 && cpuRate < 1) return "1";
    return cpuRate.toStringAsFixed(0);
  }

  ///ios获取设备已连接的Wi-Fi名称
  static Future<String> getSSID() async {
    if (Platform.isIOS) {
      return await channel.invokeMethod("getSSID");
    }
    return "";
  }


}
